package com.sinodata.bsm.cicp.finder.aix;

/**
 * <p>Title: </p>
 * <p>
 * Aix主机的发现程序的父类
 * </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author zhaoch
 * @version 1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

public abstract class AixFinder extends Finder {
    private static final Logger logger = Logger.getLogger(AixFinder.class);
    private static final String HOSTNAME_CMDSTR = "hostname";
    private String HOSTNAME = "";

    /*AIX发现程序的命令运行时间间隔*/
    protected static final int AIX_FINDER_DEFAULT_TIMEOUT = 5;

    /**
     * 获取主机名.在aix资源发现时,将设置资源描述为:主机名_资源描述
     */
    final String getHostName(String ip) {
        if (!HOSTNAME.equals("")) {
            return HOSTNAME;
        }
        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, HOSTNAME_CMDSTR, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                list.add(strLine.trim());
            }
            if (list.size() > 0) {
                HOSTNAME = list.get(0);
                return HOSTNAME;
            }
        } catch (IOException e) {
            logger.error("failed to get hostname ", e);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        return HOSTNAME;
    }
}
